#include "mypushbutton.h"
#include <QtDebug>
#include <QPropertyAnimation>

MyPushButton::MyPushButton(const QString& normalImage,const QString& pressImage,QWidget* parent):QPushButton(parent){
    this->m_NormalImage = normalImage;
    this->m_PressImage = pressImage;
    QPixmap pixmap;
    bool ret = pixmap.load(this->m_NormalImage);
    if(!ret){
        qDebug() << normalImage << "加载图片失败";
    }
    this->setFixedSize(pixmap.width(),pixmap.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pixmap);
    this->setIconSize(QSize(pixmap.width(),pixmap.height()));
}

void MyPushButton::zoom1(){
    //创建动画对象
    QPropertyAnimation* animation = new QPropertyAnimation(this,"geometry");
    //设置时间间隔 单位为ms
    animation->setDuration(200);
    //创建起始位置
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //创建结束位置
    animation->setEndValue(QRect(this->x(),this->y() + 10,this->width(),this->height()));
    //设置缓和曲线,OutBounce 为弹跳效果
    animation->setEasingCurve(QEasingCurve::OutBounce);
    //开始执行动画
    animation->start();
}

void MyPushButton::zoom2(){
    //创建动画对象
    QPropertyAnimation* animation = new QPropertyAnimation(this,"geometry");
    //设置时间间隔 单位为ms
    animation->setDuration(200);
    //创建起始位置
    animation->setStartValue(QRect(this->x(),this->y() + 10,this->width(),this->height()));
    //创建结束位置
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置缓和曲线,OutBounce 为弹跳效果
    animation->setEasingCurve(QEasingCurve::OutBounce);
    //开始执行动画
    animation->start();
}

void MyPushButton::mousePressEvent(QMouseEvent* ev){
    if(m_PressImage.size() != 0){
        QPixmap pixmap;
        bool ret = pixmap.load(m_PressImage);
        if(!ret){
            qDebug() << m_PressImage << "加载图片失败";
        }
        this->setFixedSize(pixmap.width(),pixmap.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pixmap);
        this->setIconSize(QSize(pixmap.width(),pixmap.height()));
    }
    return QPushButton::mousePressEvent(ev);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent* ev){
    if(m_NormalImage.size() != 0){
        QPixmap pixmap;
        bool ret = pixmap.load(m_NormalImage);
        if(!ret){
            qDebug() << m_NormalImage << "加载图片失败";
        }
        this->setFixedSize(pixmap.width(),pixmap.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pixmap);
        this->setIconSize(QSize(pixmap.width(),pixmap.height()));
    }
    return QPushButton::mouseReleaseEvent(ev);
}
